from django.contrib.auth.models import Group, User
from rest_framework import serializers, views


class UserSerializer(serializers.HyperlinkedModelSerializer):  # use url to link group
# class UserSerializer(serializers.ModelSerializer):
    class Meta:
        model = User
        fields = ['url', 'username', 'password', 'email', 'groups']
        # fields = ['id', 'username', 'password', 'email', 'groups']

    def create(self, validated_data: dict):
        username = validated_data.pop('username')
        email = validated_data.pop('email')
        password = validated_data.pop('password')

        return User.objects.create_user(username, email, password)

    def update(self, instance, validated_data):
        password = validated_data.pop('password', None)
        if password:
            instance.set_password(password)
        return super().update(instance, validated_data)
    
        # User().set_password()
        # User().is_authenticated

    # def data(self):
    #     ...


class GroupSerializer(serializers.HyperlinkedModelSerializer):
# class GroupSerializer(serializers.ModelSerializer):
    class Meta:
        model = Group
        fields = ['url', 'name', 'user_set']
        # fields = ['id', 'name', 'user_set']
